package org.bim.view.drag.design.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.api.dto.UpdateUserDTO;
import org.bim.view.drag.design.api.dto.UpdateUserPwdDTO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.config.DesignerLoginService;
import org.bim.view.drag.design.dao.UserMapper;
import org.bim.view.drag.design.entity.TUser;
import org.bim.view.drag.design.exception.BizCommonException;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.util.AssertUtil;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class UserController {
    private final UserMapper userMapper;
    @GetMapping("list")
    public HttpResult<List<TUser>> userList(@ApiParam(required = true) @RequestParam String keywords){
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        keywords = "%"+keywords+"%";
        Example example = new Example(TUser.class).selectProperties("name","account","id");
        example.createCriteria()
                .andLike("name",keywords)
                .orLike("account",keywords);
        Example.Criteria criteria = example.createCriteria()
                .andNotEqualTo("id",currentUser.getId());
        example.and(criteria);
        List<TUser> users = userMapper.selectByExample(example);
        return HttpResult.success(users);
    }

    @PostMapping("update")
    public HttpResult updateUserInfo(@RequestBody @Valid UpdateUserDTO dto){
        TUserVO userVO = ThreadLocalUtil.getCurrentUser();
        AssertUtil.isTrue(userVO.getAccount().equals(dto.getAccount()),new BizCommonException("无权修改",403));
        TUser user =userMapper.selectByPrimaryKey(userVO.getId());
        AssertUtil.isTrue(user!=null,"账户异常");
        if(!Objects.equals(dto.getEmail(),user.getEmail())){
            Example example =new Example(TUser.class);
            example.createCriteria().andEqualTo("email",dto.getEmail());
            int emailExist = userMapper.selectCountByExample(example);
            AssertUtil.isTrue(emailExist==0,"邮箱已被使用");
        }
        user.setName(dto.getName());
        user.setEmail(dto.getEmail());
        userMapper.updateByPrimaryKey(user);
        userVO.setName(dto.getName());
        userVO.setEmail(dto.getEmail());
        DesignerLoginService.onlineUserMap.put(userVO.getToken(),userVO);
        return HttpResult.success();
    }
    @PostMapping("updatePwd")
    public HttpResult updateUserPwd(@RequestBody @Valid UpdateUserPwdDTO dto){
        TUserVO userVO =  ThreadLocalUtil.getCurrentUser();
        TUser user =userMapper.selectByPrimaryKey(userVO.getId());
        AssertUtil.isTrue(user!=null,"账户异常");
        AssertUtil.isTrue(Objects.equals(user.getPassword(),dto.getOldPwd()),new BizCommonException("旧密码错误",403));
        user.setPassword(dto.getNewPwd());
        userMapper.updateByPrimaryKey(user);
        DesignerLoginService.onlineUserMap.remove(userVO.getToken());
        return HttpResult.success();
    }
}
